//
//  WSJTableViewSelectAnimaton.m
//  TableCellPopupDemo
//
//  Created by wsj2015 on 16/4/28.
//  Copyright © 2016年 Cloudox. All rights reserved.
//

#import "WSJTableViewSelectAnimaton.h"
//设备的宽高
#define SCREENWIDTH       [UIScreen mainScreen].bounds.size.width
#define SCREENHEIGHT      [UIScreen mainScreen].bounds.size.height
@implementation WSJTableViewSelectAnimaton
+ (void)excuteAnimationWithTableView:(UITableView *)tableView
                       withSuperView:(UIView *)superView
                       withCell:(UITableViewCell *)tableViewCell
                       withIndexPath:(NSIndexPath *)indexPath
          withCompleteBlock:(void(^)(void))completeBlock{
   
    
    // 获取选中的区域范围
    CGRect rectInTableView = [tableView rectForRowAtIndexPath:indexPath];
    CGRect sourceRect = [tableView convertRect:rectInTableView toView:[tableView superview]];
    // 实例化选中的cell视图，内容根据选中的cell内容来获取，范围使用上面获取的范围
    
    tableViewCell.frame = sourceRect;
    tableViewCell.backgroundColor = [UIColor whiteColor];
    [superView addSubview:tableViewCell];
    
    // 阴影视图
    UIView *shadowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    shadowView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    [superView addSubview:shadowView];
    // 将cell视图放在最前面
    [superView bringSubviewToFront:tableViewCell];
    
    // 实例化纯白视图，范围和cell视图一样，先做成透明
    UIView *tempView = [[UIView alloc] initWithFrame:tableViewCell.frame];
    tempView.backgroundColor = [UIColor whiteColor];
    tempView.alpha = 0;
    [superView addSubview:tempView];
    // 进行动画
    [UIView animateWithDuration:0.3 animations:^{
        // 稍微增加cell视图的大小
        tableViewCell.transform = CGAffineTransformMakeScale(1.0, 1.1);
        // 纯白视图的透明度设为不透明，这样就会覆盖cell视图
        tempView.alpha = 1;
    }];
    
    // 延迟执行
    double delayInSeconds = 0.3;
//    __block ViewController* bself = self;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [tableViewCell removeFromSuperview];
        // 进行动画
        [UIView animateWithDuration:0.3 animations:^{
            // 炸开纯白视图到全屏
            tempView.transform = CGAffineTransformMakeScale(1.0, SCREENHEIGHT / tempView.frame.size.height * 2);
        }];
    });
    
    // 延迟执行
    double delayInSeconds2 = 0.6;
    dispatch_time_t popTime2 = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds2 * NSEC_PER_SEC));
    dispatch_after(popTime2, dispatch_get_main_queue(), ^(void){
        // 进入详情界面
        if (completeBlock) {
            completeBlock();
        }
        [tempView removeFromSuperview];
        [shadowView removeFromSuperview];
        [tableViewCell removeFromSuperview];
    });
}
@end
